package com.jboa.service.impl;

import java.util.List;

import com.jboa.dao.FunctionDao;
import com.jboa.dao.RoleDao;
import com.jboa.dao.RoleFunctionDao;
import com.jboa.dao.UserRoleDao;
import com.jboa.model.Function;
import com.jboa.model.Role;
import com.jboa.model.RoleFunction;
import com.jboa.service.RoleService;


public class RoleServiceImpl implements RoleService {

	private RoleDao roleDao;
	private UserRoleDao userRoleDao;
	private RoleFunctionDao roleFunctionDao;
	private FunctionDao functionDao;
	
	
	public void setFunctionDao(FunctionDao functionDao) {
		this.functionDao = functionDao;
	}

	public void setRoleFunctionDao(RoleFunctionDao roleFunctionDao) {
		this.roleFunctionDao = roleFunctionDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	public List<Function> authorization(Role r) {
		// TODO Auto-generated method stub
		return this.functionDao.findAll();
	}

	public void changeRf(List<Integer> ids, Role r) {
		List <RoleFunction>rfs =this.roleFunctionDao
		.findByProperty("role.id", r.getId());
		//�����κι��ܣ���ֱ��ɾ��֮ǰ���е�rolefunction
		if(ids.size()==0){
			for(RoleFunction rf :rfs){
				this.roleFunctionDao.delete(rf);
			}
		}else{
			//ɾ��ȡ��Ĺ���
			for(int i=0;i<rfs.size();i++){
				RoleFunction rf= rfs.get(i);
				if( !ids.contains(rf.getFunction().getId()) ){
					this.roleFunctionDao.delete(rf);
				}
			}
			
			//��������ӵĹ���
			for(Integer id :ids){
				if(this.roleFunctionDao.findFidByRid(r).contains(id)){
					continue;
				}else{
					RoleFunction rf = new RoleFunction();
					rf.setRole(r);
					Function func = this.functionDao.findById(id);
					rf.setFunction(func);
					this.roleFunctionDao.save(rf);
				}
			}
			
		}
	}

	public Role findSingleRole(Role r) {		
		return this.roleDao.findById(r.getId());
	}

	public List<Integer> hadRole(Role r) {		
		return this.roleFunctionDao.findFidByRid(r);
	}

	public void modifyRole(Role r) {
		this.roleDao.update(r);

	}

	public void newRole(Role r) {
		roleDao.save(r);

	}

	public List<Role> operateRole() {
		return roleDao.findAll();
		
	}

	public void removeRole(Role r) {
		this.roleDao.delete(r);

	}

}
